package com.dx.financehy.http;

import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import java.util.Map;

/**
 * <pre>
 * @描述： http抽象类，继承工具类的公共函数
 * @作者：CLD
 * @创建时间：2020-10-18 00:27
 * </pre>
 **/
public class BaseHttpUtil extends HttpRequestConfig {

    /**
     * 创建请求对象 httpRequestBase
     *
     * @param Method
     * @param url
     * @return
     */
    protected static HttpRequestBase createHttpRequestBase(CustomHttpMethod Method, String url) {
        switch (Method) {
            case POST:
                return new HttpPost(url);
            case PUT:
                return new HttpPut(url);
            case DELETE:
                return new HttpDelete(url);
            default:
                return new HttpGet(url);
        }
    }

    /**
     * send请求
     *
     * @param url
     * @param header
     * @param contentType
     * @param body
     * @return
     */
    protected static String exec(String url, CustomHttpMethod method, Map<String, String> header, String contentType, String body) {
        CloseableHttpClient httpClient = HttpClientConnectFactory.getHttpClient();
        HttpRequestBase httpBase = createHttpRequestBase(method, url);
        setHeader(httpBase, header);
        setContentType(httpBase, contentType);
        if (httpBase instanceof HttpEntityEnclosingRequestBase) {
            setHttpBody((HttpEntityEnclosingRequestBase) httpBase, body);
        }
        try {
            CloseableHttpResponse response = httpClient.execute(httpBase);
            StatusLine statusLine = response.getStatusLine();
            if (statusLine == null)
                throw new HttpResponseException(0, "Null status line.");
            int statusCode = statusLine.getStatusCode();

            if (statusCode == 401) {
                throw new HttpResponseException(statusCode, "Unauthorized request");
            } else if (statusCode >= 300) {
                String exMsg = statusLine.getReasonPhrase();
                throw new HttpResponseException(statusCode, "HttpResponseException: " + exMsg);
            }
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                return EntityUtils.toString(httpEntity, "utf-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("httpGetUtil error : {}" + e.getMessage());
        }
        return null;
    }

    /**
     * send请求
     *
     * @param url
     * @param header
     * @param contentType
     * @param param
     * @return
     */
    protected static String exec(String url, CustomHttpMethod method, Map<String, String> header, String contentType, Map<String, Object> param) {
        CloseableHttpClient httpClient = HttpClientConnectFactory.getHttpClient();
        HttpRequestBase httpBase = createHttpRequestBase(method, url);
        setHeader(httpBase, header);
        setContentType(httpBase, contentType);
        try {
            if (httpBase instanceof HttpEntityEnclosingRequestBase) {
                setHttpBody((HttpEntityEnclosingRequestBase) httpBase, param);
            }
            CloseableHttpResponse response = httpClient.execute(httpBase);
            StatusLine statusLine = response.getStatusLine();
            if (statusLine == null)
                throw new HttpResponseException(0, "Null status line.");
            int statusCode = statusLine.getStatusCode();

            if (statusCode == 401) {
                throw new HttpResponseException(statusCode, "Unauthorized request");
            } else if (statusCode >= 300) {
                String exMsg = statusLine.getReasonPhrase();
                throw new HttpResponseException(statusCode, "HttpResponseException: " + exMsg);
            }
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                return EntityUtils.toString(httpEntity, "utf-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("httpGetUtil error : {}" + e.getMessage());
        }
        return null;
    }

}
